package com.watchthelan.domain;

import java.net.SocketException;

import com.watchthelan.service.network.SimpleSocketConnection;
import com.watchthelan.service.network.SocketConnection;

public class Server {
	private long id;
	private String name;
	private String description;
	private int port = 0;
	private int status = SocketConnection.STATE_UNKNOWN;
	private String hostname;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	/**
	 * Get status from database
	 * @return
	 */
	public int getStatus() {
		return status;
	}
	/**
	 * Set status
	 * @param status
	 */
	public void setStatus(int status) {
		this.status = status;
	}
	public String getHostname() {
		return this.hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	public String toString() {
		String buffer = "---\n"
			+ "Id: " + id + "\n"
			+ "Server: " + name + "\n"
			+ "Port: " + port + "\n"
			+ "Hostname: " + hostname + "\n"
			+ "Description: " + description + "\n"
			+ "Status: " + status + "\n"
			+ "---";
		return buffer;
	}
	
	/**
	 * Check status by opening a socket connection
	 * @return
	 */
	public int getRealStatus() throws SocketException {
		return new SimpleSocketConnection().test(this.hostname, this.port);
	}
}
